<?php
/**
 * 飞信机器人--功能插件--议题收集插件
 * 功能：根据议题分组的留言版
 * 数据表：
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
 支持参数：无
*/
/*
 数据表定义:
 分组表：
 
 议题表：
*/

class DiscussionCollect extends CommonPlugin
{
	var $UUID='ed68ee30-08c5-11de-8c30-0800200c9a66';
	
	public function DiscussionCollect(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	public function setPosition($pos)
	{
		$this->session->set("{$this->UUID}_position", $pos);
	}
	public function getPosition()
	{
		// In menu, pos=0, in group pos>0
		$pos = $this->session->getValue("{$this->UUID}_position");
		return $pos;		
	}
	public function getGroupList()
	{
		$sql = "select * from fx_discussion_group order by id";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$body = "<Font Face='楷体' Color='-16744320' Size='10.5'>{body}</Font>\n";
		
		$tmpBodyStr = "";
		for($i=0; $i<count($retArr); $i++)
		{
			$tmpBodyStr .= "[".($i+1)."] ".$retArr[$i]['group_name']."\n";
		}
		return str_replace("{body}", $tmpBodyStr, $body);
	}
	public function getTips()
	{
		$this->setPosition(0);
		$msg = $this->session->currentMenu['tips']."\n";
		return "\n".$msg.$this->getGroupList().$this->getFooter(',请直接回复您要参加的议题');		
	}
	
	public function enterDiscussion($input)
	{
		$sql = "select * from fx_discussion_group order by id";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$tmpBodyStr = "";
		for($i=0; $i<count($retArr); $i++)
		{
			if( intval($input) == ($i+1) )
			{
				$this->setPosition($retArr[$i]['id']);	
				return "您已经进入到 <Font Face='楷体' Color='-16744320' Size='10.5'>{$retArr[$i]['group_name']}</Font>,请直接输入您的建议\n".$this->getFooter();
			}
		}
	}
	
	public function writeDiscussion($input)
	{
		$class_id = $this->getPosition();
		$buddy_no = $this->session->buddyID;
		$buddy_name = mysql_real_escape_string($this->session->getBuddyName());
		$content = mysql_real_escape_string($input);
		$time = time(NULL);
		
		$sql = "insert into fx_discussion(class_id,buddy_no,buddy_name,content,send_time) ";
		$sql.= " values({$class_id},{$buddy_no},'{$buddy_name}','{$content}',{$time})";
		$this->session->db->execute($sql);
		
		return "谢谢您的建议。您可以继续发布建议！\n". $this->getFooter(',l-返回目录');
	}
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;

		if( $this->getPosition() == 0 )	// In main menu
		{
			return $this->enterDiscussion($input);
		}else
		{
			if($input == "l")
			{
				return $this->getTips();
			}
			return $this->writeDiscussion($input);
		}
		return "不支持的指令\n". $this->getFooter();
	}
}
?>